package com.yungu.swift.api.route.v1.common.h5;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.model.dto.CarModelValuationDto;
import com.yungu.swift.assets.driver.service.CarModelValuationService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.model.dto.OrderDto;
import com.yungu.swift.order.model.dto.OrderFareDto;
import com.yungu.swift.order.service.OrderFareService;
import com.yungu.swift.order.service.OrderService;
import com.yungu.swift.utils.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/h5/common")
public class CommonH5Controller {
    @Reference
    private CarModelValuationService carModelValuationService;
    @Reference
    private OrderFareService orderFareService;
    @Reference
    private OrderService orderService;

    @RequestMapping(value = {"/rulePrice"})
    public String rulePrice(@RequestParam String appid, HttpServletRequest request, Model model) {
        String orderUuid = request.getParameter("orderUuid");
        String cityUuid = request.getParameter("cityUuid");
        String businessTypeStr = request.getParameter("businessType");
        Integer businessType = 0;
        if (businessTypeStr != null) {
            businessType = Integer.parseInt(businessTypeStr);
        }
        Map<String, Object> map = MapUtils.build();
        //已完成订单
        if (orderUuid != null) {
            OrderDto orderDto = orderService.get(orderUuid).getData();
            if (orderDto == null) {
                return "获取不到订单信息";
            }
            //获取订单类型
            businessType = orderDto.getTypeModule();

            map.put("orderUuid", orderUuid);
            OrderFareDto orderFareDto = orderFareService.get(map).getData();
            if (orderFareDto == null) {
                return "获取不到计价信息";
            }
            map.clear();
            map.put("uuid", orderFareDto.getCarModelValuationUuid());
            CarModelValuationDto carModelsLevelDto = carModelValuationService.get(map).getData();
            if (carModelsLevelDto == null) {
                return "获取不到计价规则";
            }
            cityUuid = carModelsLevelDto.getCityUuid();
        }

        //查询计价
        map.clear();
        map.put("appid", appid);
        map.put("cityUuid", cityUuid);
        map.put("businessType", businessType);
        map.put("status", CommonConstant.STATUS_OPENED);
        List<CarModelValuationDto> ruleList = carModelValuationService.listRule(map).getData();
        if (CommonConstant.BUSINESS_TYPE_SPEC == businessType) {
            setLevelRulePriceModel(ruleList, model);
            return appid + "/priceRule";
        }
        if (CommonConstant.BUSINESS_TYPE_EXPRESS == businessType) {
            setNormalRulePriceModel(ruleList, model);
            return appid + "/priceRuleExpress";
        }
        return "order_type_error";
    }

    private void setLevelRulePriceModel(List<CarModelValuationDto> ruleList, Model model) {
        for (CarModelValuationDto c : ruleList) {
            switch (c.getModelType()) {
                case 1: {
                    if (c.getOrderTypeTime() == 1) {
                        model.addAttribute("realTimeComfort", c);
                    }
                    if (c.getOrderTypeTime() == 2) {
                        model.addAttribute("appointComfort", c);
                    }
                    if (c.getOrderTypeTime() == 3) {
                        model.addAttribute("dailyRentalComfort", c);
                    }
                    if (c.getOrderTypeTime() == 4) {
                        model.addAttribute("halfDailyRentalComfort", c);
                    }
                    if (c.getOrderTypeTime() == 5) {
                        model.addAttribute("sendMachineComfort", c);
                    }
                    if (c.getOrderTypeTime() == 6) {
                        model.addAttribute("meetMachineComfort", c);
                    }
                    break;
                }
                case 2: {
                    if (c.getOrderTypeTime() == 1) {
                        model.addAttribute("realTimeBusiness", c);
                    }
                    if (c.getOrderTypeTime() == 2) {
                        model.addAttribute("appointBusiness", c);
                    }
                    if (c.getOrderTypeTime() == 3) {
                        model.addAttribute("dailyRentalBusiness", c);
                    }
                    if (c.getOrderTypeTime() == 4) {
                        model.addAttribute("halfDailyRentalBusiness", c);
                    }
                    if (c.getOrderTypeTime() == 5) {
                        model.addAttribute("sendMachineBusiness", c);
                    }
                    if (c.getOrderTypeTime() == 6) {
                        model.addAttribute("meetMachineBusiness", c);
                    }
                    break;
                }
                case 3: {
                    if (c.getOrderTypeTime() == 1) {
                        model.addAttribute("realTimeDeluxe", c);
                    }
                    if (c.getOrderTypeTime() == 2) {
                        model.addAttribute("appointDeluxe", c);
                    }
                    if (c.getOrderTypeTime() == 3) {
                        model.addAttribute("dailyRentalDeluxe", c);
                    }
                    if (c.getOrderTypeTime() == 4) {
                        model.addAttribute("halfDailyRentalDeluxe", c);
                    }
                    if (c.getOrderTypeTime() == 5) {
                        model.addAttribute("sendMachineDeluxe", c);
                    }
                    if (c.getOrderTypeTime() == 6) {
                        model.addAttribute("meetMachineDeluxe", c);
                    }
                    break;
                }
                default:
                    break;
            }
        }
    }

    private void setNormalRulePriceModel(List<CarModelValuationDto> ruleList, Model model) {
        for (CarModelValuationDto c : ruleList) {
            if (c.getOrderTypeTime() == 1) {
                model.addAttribute("realTimeExpress", c);
            }
            if (c.getOrderTypeTime() == 2) {
                model.addAttribute("appointExpress", c);
            }
            if (c.getOrderTypeTime() == 3) {
                model.addAttribute("dailyRentalExpress", c);
            }
            if (c.getOrderTypeTime() == 4) {
                model.addAttribute("halfDailyRentalExpress", c);
            }
        }
    }

    @RequestMapping(value = {"/about"})
    public String about(@RequestParam String appid, HttpServletRequest request, Model model) {
        String version = request.getParameter("version");
        String identify = request.getParameter("identify");
        //来源：1：出租车APP，2：乘客端APP，3：专车APP
        String apptype = request.getParameter("apptype");
        model.addAttribute("versions", version);
        model.addAttribute("appid", appid);
        model.addAttribute("identify", identify);
        model.addAttribute("apptype", apptype);
        return appid + "/about";
    }

    /**
     * 司机提现协议
     *
     * @param appid   appid
     * @param request request
     * @param model   model
     * @return String
     */
    @RequestMapping({"/ruleExplain"})
    public String ruleExplain(@RequestParam String appid, HttpServletRequest request, Model model) {
        return appid + "/ruleExplain";
    }

    /**
     * 乘客充值协议
     *
     * @param appid   String
     * @param request request
     * @param model   model
     * @return String
     */
    @RequestMapping(value = {"/rechargeExplain"})
    public String rechargeExplain(@RequestParam String appid, HttpServletRequest request, Model model) {
        return appid + "/passengerRecharge";
    }
}